<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	public function index(){

        $this->display();
	}
        
        public function download() {
            header('Location:http://a.app.qq.com/o/simple.jsp?pkgname=com.zctx.zhongcetianxia');
        }
        
        public function phpinfo() {
            echo phpinfo();
        }
        
        /*
         * 临时链接，跳转到 参与众筹 页面
         */
        public function join() {
            header('Location:http://www.zhongcetianxia.com/index.php/M/Founding/lists/state/1.html');
            
        }




        /*
         * Date:2015-10-19
         * 中文转为拼音
         */
        public function topy() {
            $words = I('get.words');
            import("ORG.Util.Pinyin");
            $py = new \PinYin();
            echo $py->getAllPY("$words",',');
        }
        
        /*
         * Date:2015-10-19
         * 将数据库里边的所有 product_name 都转化为拼音
         * 
         */
        public function translate_all_cn() {
            import("ORG.Util.Pinyin");
            $py = new \PinYin();
            $DB = M('product_thirdparty');
            $map['qualified']=0;
            $res = $DB->where($map)->getField('id,product_name');
            print_r($DB->getLastSql());
            foreach ($res as $key => $value) {
                $DB->id = $key;
                $DB->product_name_pinyin = $py->getAllPY($value,',');
                $DB->save();
                echo $key.'-';
                
            }
        }
        
        /*
         * 获取关注用户列表
         * 
         */
        public function get_subcribe() {
            $jssdk = new \Com\JSSDK(C('appid'),C('appsecret'));
            $accesstoken = $jssdk->returnAccessToken();
        
            $wechat = new \Com\WechatAuth(C('appid'),C('appsecret'),$accesstoken);
            $users = $wechat->userGet();
//            print_r($users['data']['openid']);
            foreach ($users['data']['openid'] as $key => $value) {
                $datalist[]=array('openid'=>$value,'issubscribe'=>1,'ctime'=>I('server.REQUEST_TIME'),'utime'=>I('server.REQUEST_TIME'));
            }
            $DB = M("members_subscribe_records");
            $DB->addAll($datalist);
        }
        
        
        public function prize() {
            $random_num = rand(1,29);
            
            echo "众测天下年终幸运中奖号码是：".$random_num;
            echo "<br /><a href='http://www.zhongcetianxia.com/index.php/Home/Index/prize'>再来一次</a>";
            
        }



        public  function set_urldecode(){

            $DB = m('product');
            $keys = '\%'.'%';
            $map['name'] = array('like',$keys);

            $list = $DB->where($map)->select();
            // print_r($list); die;
            foreach ($list as $key => $value) {
                # code...
                $list[$key]['name'] = urldecode($value['name']);
                $list[$key]['brand'] = urldecode($value['brand']);
            }

            foreach ($list as $key => $value) {
                $DB->where(array('id' => $value['id']))->data(array('name' => $value['name'],'brand' => $value['brand']))->save();
            }
        }



        /**上传第三方数据**/
        public function upload_third_party(){

            if(IS_POST){

                $upload_url = '';
                $geturl = './Uploads/'.$this->upload($_FILES['third_data'],$upload_url);
                // echo $geturl; die;

                //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
                import("Org.Util.PHPExcel");
                //要导入的xls文件，位于根目录下的Public文件夹
                $filename=$geturl;
                //创建PHPExcel对象，注意，不能少了\
                $PHPExcel=new \PHPExcel();
                //如果excel文件后缀名为.xls，导入这个类
                import("Org.Util.PHPExcel.Reader.Excel5");
                //如果excel文件后缀名为.xlsx，导入这下类
                //import("Org.Util.PHPExcel.Reader.Excel2007");
                //$PHPReader=new \PHPExcel_Reader_Excel2007();

                $PHPReader=new \PHPExcel_Reader_Excel5();
                //载入文件
                $PHPExcel=$PHPReader->load($filename);
                //获取表中的第一个工作表，如果要获取第二个，把0改为1，依次类推
                $currentSheet=$PHPExcel->getSheet(0);
                //获取总列数
                $allColumn=$currentSheet->getHighestColumn();
                //获取总行数
                $allRow=$currentSheet->getHighestRow();
                //循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始
                for($currentRow=2;$currentRow<=$allRow;$currentRow++){
                    //从哪列开始，A表示第一列
                    for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){
                        //数据坐标
                        $address=$currentColumn.$currentRow;
                        //读取到的数据，保存到数组$arr中
                        $arr[$currentRow][$currentColumn]=$currentSheet->getCell($address)->getValue();
                    }
                }
                // print_r($arr); die;
                $i = 0;
                foreach ($arr as $key => $value) {
                    # code..
                    
                    $data[$i]['control_no'] = $value['A'];
                    $data[$i]['firm_name'] = $value['B'];
                    $data[$i]['firm_address'] = $value['C'];
                    $data[$i]['department_name'] = $value['D'];
                    $data[$i]['location'] = $value['E'];
                    $data[$i]['province'] = $value['F'];
                    $data[$i]['city'] = $value['G'];
                    $data[$i]['product_name'] = $value['H'];
                    $data[$i]['s_category'] = $value['I'];
                    $data[$i]['b_category'] = $value['J'];
                    $data[$i]['brand'] = $value['K'];
                    $data[$i]['short_product_name'] = $value['L'];
                    $data[$i]['product_model'] = $value['M'];
                    $data[$i]['cdate'] = $value['N'];
                    $data[$i]['qualified'] = $value['O'];
                    $data[$i]['item_category'] = $value['P'];
                    $data[$i]['items'] = $value['Q'];
                    $data[$i]['check_result'] = $value['R'];
                    $data[$i]['standard'] = $value['S'];
                    $data[$i]['barcode'] = $value['T'];
                    $data[$i]['bulletin_code_num'] = $value['U'];
                    $data[$i]['bulletin_code'] = $value['V'];
                    $data[$i]['bctime'] = $value['W'];
                    $data[$i]['check_items'] = $value['X'];
                    $data[$i]['batch_no'] = $value['Y'];
                    $data[$i]['origin'] = $value['Z'];
                    $data[$i]['institution'] = $value['AA'];
                    $data[$i]['remark'] = $value['AB'];

                                
                    $i++;
                }

                // print_r($data); die;
                $DB_import_excel = M('third_party_product');
                // var_dump($DB_import_excel); die;
                $res = $DB_import_excel->addAll($data);
                if(!empty($res)){

                    echo '导入成功!';
                }else{

                    echo '导入失败!';
                }

            }else{

                $this->display();
            }
        }


        /**
         * 单文件上传
         */
        public function upload($file,$upload_url){    
            $upload = new \Think\Upload();// 实例化上传类    
            $upload->maxSize   =     3145728 ;// 设置附件上传大小    
            $upload->exts      =     array('xls');// 设置附件上传类型    
            $upload->savePath  =     $upload_url; // 设置附件上传目录    
            // 上传单个文件     
            $info   =   $upload->uploadOne($file);    
            if(!$info) {
                // 上传错误提示错误信息        
                $this->error($upload->getError());    
                }else{
                // 上传成功 获取上传文件信息         
                return $info['savepath'].$info['savename'];    
            }
        }
        
}
?>
